// 上周素材合作spu
<template>
    <div class="gradient-content spu full-width">
        <div :id="idName"></div>
    </div>
</template>

<script>
import { Chart } from '@antv/g2'

export default {
    props: {
        idName: {
            type: String,
            default: 'spuChart',
        },
        chartData: {
            type: Array,
            default: () => [],
        },
        shop_id: {
            type: [String, Number],
            default: '17',
        },
    },
    data() {
        return {
            chart: null,
            height: 0,
        }
    },
    watch: {
        // chartData(val) {
        //     this.chart.changeData(val)
        // },
        chartData: {
            handler(val) {
                if (val.length > 0) {
                    this.$nextTick(() => {
                        if (this.height == 0) {
                            const boxContElement = document.querySelector(`.gradient-content.spu `)
                            this.height = boxContElement.clientHeight
                        }
                        if (this.chart) {
                            this.chart.destroy()
                        }
                        this.render()
                    })
                }
            },
            immediate: true,
            deep: true,
        },
    },
    mounted() {
        // this.$nextTick(() => {
        //     const boxContElement = document.querySelector(`.gradient-content`)
        //     this.height = boxContElement.clientHeight
        //     this.render()
        // })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.destroy()
    },
    methods: {
        render() {
            // Step 1: 创建 Chart 对象
            this.chart = new Chart({
                container: this.idName,
                height: this.height,
                autoFit: true,
                padding: [15, 10, 30, 50],
            })

            // Step 2: 载入数据源
            this.chart.data(this.chartData)

            // Step 3: 创建图形语法，定义 Position 和 Color
            this.chart.scale('sales', {
                nice: true,
            })

            this.chart.tooltip({
                showMarkers: false,
            })
            this.chart.interaction('active-region')
            const color = this.shop_id == 18 ? '#2db2ff' : '#8f7cff'
            this.chart.interval().position('year*sales').color(color)

            // Step 4: 渲染图表
            this.chart.render()
        },
    },
}
</script>
